//
//  HXDGoodsIntroViewController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/27.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDGoodsIntroViewController.h"
#import "HXDGoodsIntroModel.h"
#import "HXDGoodsIntroTableViewCell.h"
#import "HXDShopIntroTableViewCell.h"
#import "HXDLikeTableViewCell.h"
#import "HXDLikeModel.h"

@interface HXDGoodsIntroViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *goodsIntro;
@property (strong, nonatomic) NSArray *like;

@end

@implementation HXDGoodsIntroViewController

- (NSArray *)goodsIntro {
    if (_goodsIntro == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"good_info" ofType:@"plist"];
        NSArray *temp = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *models = [NSMutableArray array];
        for (NSDictionary *dict in temp) {
            HXDGoodsIntroModel *model = [HXDGoodsIntroModel goodsIntroWithDict:dict];
            [models addObject:model];
        }
        _goodsIntro = models;
    }
    return _goodsIntro;
}

- (NSArray *)like {
    if (_like == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"guess_like" ofType:@"plist"];
        NSArray *temp = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *models = [NSMutableArray array];
        for (NSDictionary *dict in temp) {
            HXDLikeModel *model = [HXDLikeModel likeWithDict:dict];
            [models addObject:model];
        }
        _like = models;
    }
    return _like;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"商品详情";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orLogin:) name:@"isLogin" object:nil];
}

- (void)orLogin:(NSNotification *)notification {
    if (notification.object) {
        self.rightButton.frame = CGRectMake(kUIScreenWidth - 2 * kNavigationButtonWidth + 30, kUIStatusBarHeight, kNavigationButtonWidth, kNavigationButtonWidth);
        [self.rightButton setTitle:nil forState:UIControlStateNormal];
        UIImage *icon = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
        [self.rightButton setBackgroundImage:icon forState:UIControlStateNormal];
        self.rightButton.layer.cornerRadius = 20;
        self.rightButton.clipsToBounds = YES;
        self.rightButton.userInteractionEnabled = NO;
    }
}

- (void)backMethod {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 1;
    } else {
        return self.like.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXDGoodsIntroTableViewCell *cell = [HXDGoodsIntroTableViewCell goodsIntroTableViewCell];
        cell.goodsIntro = self.goodsIntro[0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 1) {
        HXDShopIntroTableViewCell *cell = [HXDShopIntroTableViewCell shopIntroTableViewCell];
        cell.goodsIntro = self.goodsIntro[0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        HXDLikeTableViewCell *cell = [HXDLikeTableViewCell likeTableViewCell];
        cell.like = self.like[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 200;
    } else if (indexPath.section == 1) {
        return 80;
    } else {
        return 105;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shop"]];
        return imageView;
    } else if (section == 2) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guess"]];
        return imageView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 35;
    }
}

@end
